package com.example.lixudongdazuoye.ui.news;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.lixudongdazuoye.MakeMomentPage;
import com.example.lixudongdazuoye.R;
import com.example.lixudongdazuoye.component.NewsAdapter;
import com.example.lixudongdazuoye.domain.Friends;
import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.appbar.CollapsingToolbarLayout;

import java.util.ArrayList;
import java.util.List;

public class NewsFragment extends Fragment implements View.OnClickListener {

    private NewsViewModel newsViewModel;
    private List<Friends> list;
    private static NewsAdapter adapter;
    private ImageView goMakeMoment;
    private List<Integer> profiles;
    private List<String> nickname;
    private List<List<Integer>> itemPictures;
    private List<Integer> itemPicture;
    private List<String> copyWriting;
    private List<Integer> times;
    private static boolean flag=false;


    // onCreateView创建对应的视图
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        newsViewModel =
                new ViewModelProvider(this).get(NewsViewModel.class);
        View root = inflater.inflate(R.layout.fragment_news, container, false);
        newsViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {

            }
        });


        AppBarLayout appBarLayout = root.findViewById(R.id.appBar);
        appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                CollapsingToolbarLayout collapsingToolbar = root.findViewById(R.id.collapsing_toolbar);
                int color = Color.argb(200,0,0,0);
                collapsingToolbar.setCollapsedTitleTextColor(color);
                ImageView imageView = root.findViewById(R.id.image1);
                if (Math.abs(verticalOffset) >= appBarLayout.getTotalScrollRange()) { // 折叠状态
                    collapsingToolbar.setTitle("朋友圈");
                    imageView.setVisibility(View.GONE);
                } else { // 非折叠状态
                    collapsingToolbar.setTitle("");
                    imageView.setVisibility(View.VISIBLE);
                }
            }
        });
        initFriends();
        RecyclerView recyclerView = (RecyclerView) root.findViewById(R.id.recycler_view);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
        recyclerView.setLayoutManager(layoutManager);
        adapter = new NewsAdapter(list,profiles,nickname,itemPictures,copyWriting,times);
        recyclerView.setAdapter(adapter);
        goMakeMoment=root.findViewById(R.id.camera_id);
        goMakeMoment.setOnClickListener(this);
        return root;
    }
    @Override
    public void onClick(View v) {
        Intent intent=new Intent(getActivity(), MakeMomentPage.class);
        startActivity(intent);

    }
    private void initFriends() {
        list=new ArrayList<>();
        if(flag)
            list.add(new Friends("黎沈云"));
        list.add(new Friends("扒犁"));
        list.add(new Friends("咕噜噜噜"));
        list.add(new Friends("子曜"));
        list.add(new Friends("饼酱"));
        list.add(new Friends("差不多是个费喵了"));
        list.add(new Friends("瓜"));
        list.add(new Friends("榴莲人"));
        list.add(new Friends("真正的高手，连自己都看不透"));
        list.add(new Friends("七月"));
        list.add(new Friends("晴天落叶"));
        list.add(new Friends("妖梦"));
        list.add(new Friends("你的恨还不够"));
        list.add(new Friends("雪月风花"));
        list.add(new Friends("Esin"));//count
        list.add(new Friends("御影龙斩"));
        list.add(new Friends("弱者为什么要战斗"));
        list.add(new Friends("睿智的眼神"));
        list.add(new Friends("分来"));
        list.add(new Friends("苦涩"));
        list.add(new Friends("泷夜"));
        list.add(new Friends("苏玉成"));
        list.add(new Friends("桐姥爷"));
        list.add(new Friends("月"));
        list.add(new Friends("醉生梦死"));

        nickname=new ArrayList<>();
        if(flag)
            nickname.add("黎沈云");
        nickname.add("扒犁");
        nickname.add("咕噜噜噜");
        nickname.add("子曜");
        nickname.add("饼酱");
        nickname.add("差不多是个费喵了");
        nickname.add("瓜");
        nickname.add("榴莲人");
        nickname.add("真正的高手，连自己都看不透");
        nickname.add("七月");
        nickname.add("晴天落叶");
        nickname.add("妖梦");
        nickname.add("你的恨还不够");
        nickname.add("雪月风花");
        nickname.add("Esin");
        nickname.add("御影龙斩");
        nickname.add("弱者为什么要战斗");
        nickname.add("睿智的眼神");
        nickname.add("分来");//19
        nickname.add("苦涩");
        nickname.add("泷夜");
        nickname.add("苏玉成");
        nickname.add("桐姥爷");
        nickname.add("月");
        nickname.add("醉生梦死");
        profiles=new ArrayList<>();//朋友圈好友的头像
        if(flag)
            profiles.add(R.drawable.p16);
        profiles.add(R.drawable.p12);
        profiles.add(R.drawable.p13);
        profiles.add(R.drawable.p8);
        profiles.add(R.drawable.p10);
        profiles.add(R.drawable.p7);
        profiles.add(R.drawable.p2);
        profiles.add(R.drawable.p4);
        profiles.add(R.drawable.p3);//9
        profiles.add(R.drawable.p12);
        profiles.add(R.drawable.p12);
        profiles.add(R.drawable.p12);
        profiles.add(R.drawable.p12);
        profiles.add(R.drawable.p12);
        profiles.add(R.drawable.p12);
        profiles.add(R.drawable.p12);
        profiles.add(R.drawable.p12);
        profiles.add(R.drawable.p12);
        profiles.add(R.drawable.p12);
        profiles.add(R.drawable.p12);
        profiles.add(R.drawable.p12);
        profiles.add(R.drawable.p12);
        profiles.add(R.drawable.p12);
        profiles.add(R.drawable.p12);
        profiles.add(R.drawable.p12);

        times=new ArrayList<>();//发布动态距离现在的时间
        if(flag)
            times.add(4);
        times.add(25);
        times.add(35);
        times.add(50);
        times.add(100);
        times.add(200);
        times.add(500);
        times.add(700);
        times.add(800);
        times.add(900);
        times.add(950);
        times.add(1000);
        times.add(1100);
        times.add(1200);
        times.add(1300);
        times.add(1400);
        times.add(1500);
        times.add(1600);
        times.add(1700);
        times.add(1800);
        times.add(2000);
        times.add(2500);
        times.add(3000);
        times.add(5000);
        times.add(8000);


        itemPictures=new ArrayList<>();
        itemPicture=new ArrayList<>();
        if(flag){
            itemPictures.add(itemPicture);
        }

        itemPicture=new ArrayList<>();
        itemPicture.add(R.drawable.pyq1);
        itemPictures.add(itemPicture);

        itemPicture=new ArrayList<>();
        itemPicture.add(R.drawable.pyq2_1);
        itemPicture.add(R.drawable.pyq2_2);
        itemPictures.add(itemPicture);

        itemPicture=new ArrayList<>();
        itemPicture.add(R.drawable.pyq3_1);
        itemPicture.add(R.drawable.pyq3_2);
        itemPictures.add(itemPicture);

        itemPicture=new ArrayList<>();
        itemPicture.add(R.drawable.pyq4_1);
        itemPicture.add(R.drawable.pyq4_2);
        itemPicture.add(R.drawable.pyq4_3);
        itemPictures.add(itemPicture);

        itemPicture=new ArrayList<>();
        itemPicture.add(R.drawable.pyq5_1);
        itemPicture.add(R.drawable.pyq5_2);
        itemPictures.add(itemPicture);

        itemPicture=new ArrayList<>();
        itemPicture.add(R.drawable.pyq6);
        itemPictures.add(itemPicture);

        itemPicture=new ArrayList<>();
        itemPicture.add(R.drawable.pyq7);
        itemPictures.add(itemPicture);

        itemPicture=new ArrayList<>();
        itemPicture.add(R.drawable.pyq8_1);
        itemPicture.add(R.drawable.pyq8_2);
        itemPictures.add(itemPicture);

        itemPicture=new ArrayList<>();
        itemPicture.add(R.drawable.pyq7);
        itemPictures.add(itemPicture);

        itemPicture=new ArrayList<>();
        itemPicture.add(R.drawable.pyq6);
        itemPictures.add(itemPicture);

        itemPicture=new ArrayList<>();
        itemPicture.add(R.drawable.p2);
        itemPictures.add(itemPicture);

        itemPicture=new ArrayList<>();
        itemPicture.add(R.drawable.p3);
        itemPictures.add(itemPicture);

        itemPicture=new ArrayList<>();
        itemPicture.add(R.drawable.p4);
        itemPictures.add(itemPicture);

        itemPicture=new ArrayList<>();
        itemPicture.add(R.drawable.p5);
        itemPictures.add(itemPicture);

        itemPicture=new ArrayList<>();
        itemPicture.add(R.drawable.p6);
        itemPictures.add(itemPicture);

        itemPicture=new ArrayList<>();
        itemPicture.add(R.drawable.p7);
        itemPictures.add(itemPicture);

        itemPicture=new ArrayList<>();
        itemPicture.add(R.drawable.p8);
        itemPictures.add(itemPicture);

        itemPicture=new ArrayList<>();
        itemPicture.add(R.drawable.p9);
        itemPictures.add(itemPicture);

        itemPicture=new ArrayList<>();
        itemPicture.add(R.drawable.pyq7);
        itemPictures.add(itemPicture);

        itemPicture=new ArrayList<>();
        itemPicture.add(R.drawable.pyq7);
        itemPictures.add(itemPicture);

        itemPicture=new ArrayList<>();
        itemPicture.add(R.drawable.pyq7);
        itemPictures.add(itemPicture);

        itemPicture=new ArrayList<>();
        itemPicture.add(R.drawable.pyq7);
        itemPictures.add(itemPicture);

        itemPicture=new ArrayList<>();
        itemPicture.add(R.drawable.pyq7);
        itemPictures.add(itemPicture);

        itemPicture=new ArrayList<>();
        itemPicture.add(R.drawable.pyq7);
        itemPictures.add(itemPicture);

        copyWriting=new ArrayList<>();
        if(flag)
            copyWriting.add("hello");
        copyWriting.add("嗐");
        copyWriting.add("阿巴阿巴");
        copyWriting.add("。。。");
        copyWriting.add("呃");
        copyWriting.add("心跳文学部");
        copyWriting.add("柚子厨");
        copyWriting.add("可以");
        copyWriting.add("啊");
        copyWriting.add("无所谓");
        copyWriting.add("可笑");
        copyWriting.add("艾雅法拉");
        copyWriting.add("很好");
        copyWriting.add("不错");
        copyWriting.add("祝福");
        copyWriting.add("膈应");
        copyWriting.add("歇心");
        copyWriting.add("奥斯卡经过的地方");
        copyWriting.add("阿斯女友");
        copyWriting.add("旗下营销部女款");
        copyWriting.add("埃斯珀");
        copyWriting.add("啊啊啊啊啊啊啊啊啊啊啊");
        copyWriting.add("哼哼，啊啊啊啊啊啊啊啊啊啊啊");
        copyWriting.add("恶臭");
        copyWriting.add("可恶");

    }

    public static void update(){
        flag=true;
        adapter.notifyDataSetChanged();
    }

}




